/************************************************************************/
// Copyright (C) 2016, han_gangbiao. All rights reserved.
// Use of this source code is governed by a GPL-v2 license that can be found in the License file.
// 
// [Time]:      2016-3-20 16:43:50
// [Author]:    han_gangbiao [http://my.oschina.net/jackhen]
// [Info]:      
/************************************************************************/

#include "RPCClient.h"

int main(int argc, char* argv[])
{
    const char* ip = NULL;
    int port = 0;
    if(argc > 2) {
        ip = argv[1];
        port = atoi(argv[2]);
    } else {
        fprintf(stderr, "Usage: %s server_ip server_port\n", argv[0]);
        return -1;
    }
    
    if(port <= 0 || port >= 65536)
    {
        fprintf(stderr, "Usage: %s server_ip server_port\n", argv[0]);
        return -1;
    }
    
    CRpcClientMgr mgr; //mgr用栈对象貌似退出有问题，main会比mgr早退出,进程会卡住, 堆上无问题
    //由于主线程不会join其他线程(mgr中netReactor的线程池)的退出,main退出后,netReactor还在run,导致整个进程出现异常，建议mgr提供一个主动接口(内部uninit)
    //CRpcClientMgr* mgr = new CRpcClientMgr();
    //smart::ref_ptr<CRPCClient> client = new CRPCClient(&mgr);
    //multiplex manager
    //int ret = client->start_rpc_client(ip, port);
    //if(ret < 0) {
    //   printf("start rpc-client[%s:%d] Failed!\n", ip, port);
    //   return -1;
    //} else {
    //    printf("start rpc-client[%s:%d] OK!\n", ip, port);
    //}
    std::string remote_ip = ip;
    int remote_port = port;
    CAutoReconnSingleton::instance()->start_run(remote_ip, remote_port, &mgr);

    char c;
	do{
		c = getchar();
		SLEEP_MS(100);

		printf("Press q or Q to exit...\n");
	}while(c!='q' && c!='Q');
    //SAFE_DELETE(client);
    //SAFE_DELETE(mgr);
    CAutoReconnSingleton::instance()->stop_run();
    mgr.free();
    printf("exit main......\n");
    return 0;
}